package com.volatileshooter.state;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import de.lessvoid.nifty.EndNotify;
import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import de.lessvoid.nifty.slick.NiftyGameState;

/**
 * MenuState.
 * @author void
 */
public class MenuState extends NiftyGameState implements ScreenController {

	/**
	 * myId.
	 */
	public static int id = StateIdProvider.getNextId();

	/**
	 * current game container.
	 */
	private GameContainer container;

	/**
	 * game.
	 */
	private StateBasedGame game;

	/**
	 * current screen.
	 */
	private Screen screen;

	/**
	 * Create the MenuState.
	 * @param newContainer GameContainer
	 * @param newGame game
	 * @param filename nifty file to load
	 */
	public MenuState(final GameContainer newContainer, final StateBasedGame newGame, final String filename) throws SlickException {
		super(id);
		game = newGame;
		container = newContainer;
		fromXml(filename, this);
		enableMouseImage(new Image("crosshair.png"));
	}

	/**
	 * Bind this ScreenController to a screen.
	 * @param newNifty nifty
	 * @param newScreen screen
	 */
	public void bind(final Nifty newNifty, final Screen newScreen) {
		screen = newScreen;
	}
	
	/**
	 * on start screen.
	 */
	public void onStartScreen() {
		if (screen.getScreenId().equals("start")) {
			nifty.gotoScreen("mainMenu");
		} else if (screen.getScreenId().equals("newGame")) {
			screen.findElementByName("newGame").setFocus();
		}
	}

	/**
	 * on end screen.
	 */
	public void onEndScreen() {
	}

	/**
	 * new game.
	 */
	public void newGame() {
		screen.endScreen(new EndNotify() {
			public void perform() {
				game.enterState(GameState.id);
			}
		});
	}

	public void joinGame() {
		nifty.gotoScreen("joinGame");
	}
	
	public void exit() {
		screen.endScreen(new EndNotify() {
			public void perform() {
				container.exit();
			}
		});		
	}
	
	public void connect() {
		
	}
	
	public void cancelConnect() {
		nifty.gotoScreen("mainMenu");
	}
}
